from django.contrib.auth.models import User
from django.db import models
from utils.modelsMixin import ModelMixin
# Create your models here.


class Userdetail(ModelMixin):
    SEX_CHOICES = (
        (1, '男'),
        (0, '女'),
    )
    # 头像
    avatar = models.TextField(blank=True, null=True, verbose_name='头像')
    # 手机号
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name='手机号')
    age = models.IntegerField(blank=True, null=True, verbose_name='年龄')
    # 性别
    sex = models.IntegerField(choices=SEX_CHOICES, blank=True, null=True, verbose_name='性别')
    # 生日
    birthday = models.DateField(blank=True, null=True, verbose_name='生日')
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    class Meta:
        db_table = 'userdetail'


# 用户的收货地址
# id     name    parent
# 1      湖南省    null
# 2      长沙市    1
# 3      雨花区    2

class Area(models.Model):
    name = models.CharField(max_length=20, verbose_name="名称")
    # 建立外键约束  通过self  关联自身
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True, verbose_name="上级行政区划")

    class Meta:
        db_table = "area"
        # admin后台显示的名称
        verbose_name_plural = "行政区划"

    def __str__(self):
        return self.name


# 详情地址表
class Address(ModelMixin):
    name = models.CharField(max_length=40, verbose_name='地址名')
    receiver = models.CharField(max_length=40, verbose_name='收货人')
    province = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='省', related_name='province_addresses')
    city = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='市', related_name='city_addresses')
    district = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='区', related_name='district_addresses')
    place = models.CharField(max_length=40, verbose_name='详情地址')
    mobile = models.CharField(max_length=11, verbose_name='手机')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        # 根据更新时间排序，最新的放最前面
        ordering = ['-updated_time']
        db_table = 'address'
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name
